package com.lagou.controller;

import com.lagou.pojo.LagouAuthCode;
import com.lagou.service.CodeService;
import com.lagou.service.EmailServiceFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 5:56 下午 2022/1/31
 */
@RestController
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private CodeService codeService;
    @Autowired
    private EmailServiceFeignClient emailServiceFeignClient;

    /**
     * 生成验证码
     * @param email
     * @return
     */
    @GetMapping("/create/{email}")
    public Boolean create(@PathVariable("email") String email){
        LagouAuthCode code = codeService.createCode(email);
        Boolean result= emailServiceFeignClient.sendMail(email, code.getCode());
        if(!result){
            System.out.println("验证码发送失败");
            return false;
        }
        System.out.println("验证码发送成功");
        return true;
    }

    @GetMapping("/validate/{email}/{code}")
    public Integer validate(@PathVariable("email") String email,@PathVariable("code") String code){
        return codeService.validateCode(email,code);
    }
}
